package cn.dqy.gotanserver.support.msghandle.impl;

import cn.dqy.gotanserver.config.ConfigClass;
import cn.dqy.gotanserver.dto.GoTanInet;
import cn.dqy.gotanserver.dto.GoTanObject;
import cn.dqy.gotanserver.support.ClientRespondRecordUtils;
import cn.dqy.gotanserver.support.msghandle.MsgProcessInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

//处理通用消息
@Component
public class SimpleMsgProcess implements MsgProcessInterface {
    static Logger logger = LoggerFactory.getLogger(SimpleMsgProcess.class);

    @Autowired
    ConfigClass configClass;

    @Autowired
    ClientRespondRecordUtils clientRespondRecordUtils;

    @Override
    public void process(GoTanInet inetPack, GoTanObject headPack, String ipAndPortKey) {
        logger.info("普通消息处理");
        String fileRootPath = configClass.getFileRootPath();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String dateStr = format.format(new Date());

        if(headPack.isHasFile()){ //包含文件
            logger.info("该信息包含文件");
            String fileName="";
            if(!headPack.isNameIsTrue()) fileName= dateStr + "-CUT" + configClass.getPicType(); //图片名称格式 :val + 时间 + ip:port
            else fileName = headPack.getFileName() + "." + headPack.getFileFixName();
            String filePath = fileRootPath + fileName;
            saveFile(filePath,inetPack.getPic_data());
            clientRespondRecordUtils.wInfo("取到文件 " + filePath,true,true);
        }else {
            logger.info("该信息不包含文件");
            logger.info("remark = {}",headPack.getRemark());
            clientRespondRecordUtils.wInfo(headPack.getRemark(),true,false);
        }
    }
}
